import 'package:flutter/cupertino.dart'; //CupertinoPageRoute页面路由风格，向左推入，向右退出
import '../pages/news_detail.dart';
import '../pages/pic_detail.dart';
import '../pages/dialog.dart';
import '../pages/page_view_detail.dart';
import '../pages/pageView/page_example.dart';
import '../pages/pageView/reach_bottom_load.dart';
import '../pages/pageView/page_view_swiper.dart';
import '../pages/pageView/page_fit_content.dart';
import '../main.dart';

//1、命名路由列表
final Map routes = {
  '/': (context) => const HomePage(),
  '/news_detail': (context, {arguments}) =>
      NewsDetail(arguments: arguments), //带参数的命名路由
  '/pic_detail': (context) => const PicDetail(),//不带参数的命名路由
  '/dialog_detail':(context)=>const Dialog(),
  '/page_view_detail':(context)=>const PageViewDetail(),
  '/page_example':(context)=>const PageExample(),
  '/page_load_more':(context)=>const ReachBottomLoad(),
  '/pageViewSwiper':(context)=>const PageViewSwiper(),
  '/page_fit_content':(context)=>const PageFitContent()
};

//2、跳转守卫
var onGenerateRoute= (RouteSettings settings) {
  final String? name = settings.name; //name属性就是当前跳转的路径，例如'/news_detail'这种路径
  final Function? pageContentBuilder = routes[name]; //路径对应的函数
  if (pageContentBuilder != null) {
    if (settings.arguments != null) {
      //传递参数的。例如路径对应的函数为：(context,{arguments})=> NewsDetail(arguments:arguments)
      final Route route = CupertinoPageRoute(
          builder: (context) =>
              pageContentBuilder(context, arguments: settings.arguments));
      return route;
    } else {
      //无参数的。例如路径对应的函数为：(context)=> NewsDetail()
      final Route route =
          CupertinoPageRoute(builder: (context) => pageContentBuilder(context));
      return route;
    }
  }
  return null;
};
